<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var kWeakestRows = function (mat, k) {
      let arr = [];
      let Arrs = [];
      // 计算每行战斗力 同时记录行数 组成二维数组
      for (let i = 0; i < mat.length; i++) {
        arr.push([i, mat[i].filter((item) => item === 1).length]);
      }
      // 根据战斗力来进行排序
      arr.sort((a, b) => a[1] - b[1]);
      // 根据要求循环遍历取规定个数
      for (let i = 0; i < k; i++) {
        Arrs.push(arr[i][0]);
      }
      return Arrs;
    };
    console.log(kWeakestRows([[1, 1, 0, 0, 0],
    [1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0],
    [1, 1, 0, 0, 0],
    [1, 1, 1, 1, 1]], 3));
  </script>
</body>

</html>